package tech.aistar.Conteoller;


import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.model.dto.Code;
import tech.aistar.model.dto.Result;
import tech.aistar.model.entity.Customer;
import tech.aistar.service.ICustomerService;
import tech.aistar.util.EmailSendUtil;
import tech.aistar.util.JWTUtils;

import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private ICustomerService iCustomerService;

    @Autowired
    private EmailSendUtil emailSendUtil;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping()
    public String login(){
        return "login/Login";
    }

    @ResponseBody
    @GetMapping("sendcode")
    public Result index(HttpServletRequest request, String email){
        Customer customer=iCustomerService.getByEmail(email);

        if(customer==null){
            return new Result(Code.BAD,"邮箱不存在");
        }
        int codeInt = (int) (Math.random()*90000+100000);
        String code = String.valueOf(codeInt);

        //两份钟有效期
        redisTemplate.opsForValue().set(email,code,1, TimeUnit.MINUTES);
        emailSendUtil.emailSend("1807074188@qq.com","孤勇直聘验证码","验证码"+code,email);
        return new Result(Code.OK,"验证码发送成功",code);
    }

    @ResponseBody
    @PostMapping("emaillogin")
    public Result emailLogin(String email,String code, HttpServletRequest request){
        Customer customer=iCustomerService.getByEmail(email);

        if(customer==null){
            return new Result(Code.BAD,"邮箱不存在");
        }

        //尝试获取code
        //String codeSes = (String) request.getSession().getAttribute(email);
        if(!redisTemplate.hasKey(email)){
            return new Result(Code.EXPIRED,"验证码过期!");
        }
        String codeRedis = (String) redisTemplate.opsForValue().get(email);

        if(!code.equals(codeRedis)){
            return new Result(Code.WRONG,"验证码不正确!");
        }

        redisTemplate.opsForValue().set("customer",customer);
        //①创建token
        String token = JWTUtils.geneJsonWebToken(customer);
        //②将这个token返回给客户端
        //因此这个token是绝对不能包含业务中的敏感的数据的.

        return new Result(Code.OK,"登录成功",token);
    }

    @ResponseBody
    @PostMapping("accountlogin")
    public Result accountLogin(String username,String password, HttpServletRequest request){
        Customer customer=iCustomerService.getByName(username);
        if(customer==null){
            return new Result(Code.BAD,"用户不存在");
        }
        if (!password.equals(customer.getPassword())){
            return new Result(Code.WRONG,"密码错误");
        }
        redisTemplate.opsForValue().set("customer",customer);
        //①创建token
        String token = JWTUtils.geneJsonWebToken(customer);
        return new Result(Code.OK,"登录成功",token);
    }


    @GetMapping("signout")
    public String signOut(){
        redisTemplate.delete("customer");
        return "index/index";
    }

}
